#include "global.h"

using namespace std;
Request request[MAX_REQUEST_NUM];
Object object[MAX_OBJECT_NUM];

int T, M, N, V, G, K1, K2;
int disk[MAX_DISK_NUM][MAX_DISK_SIZE],unit_num[MAX_DISK_NUM][MAX_DISK_SIZE];
DiskPoint disk_point[MAX_DISK_NUM][2];
int n_deletes[MAX_TIME_SLICING];
int n_writes[MAX_TIME_SLICING];
int n_reads[MAX_TIME_SLICING];
int num_read[MAX_TAG_NUM][MAX_TIME_SLICING];
int num_write[MAX_TAG_NUM][MAX_TIME_SLICING];
int num_delete[MAX_TAG_NUM][MAX_TIME_SLICING];
int sum[MAX_TAG_NUM][MAX_TIME_SLICING*2];
vector<int> delete_datas[MAX_TIME_SLICING];
vector<tuple<int, int, int>> write_datas[MAX_TIME_SLICING];
vector<tuple<int, int>> read_datas[MAX_TIME_SLICING];

int dis_canread[MAX_G];

void clean()
{
    for (auto& obj : object) {
        for (int i = 1; i <= REP_NUM; i++) {
            if (obj.unit[i] == nullptr)
                continue;
            free(obj.unit[i]);
            obj.unit[i] = nullptr;
        }
    }
}

void incre_action()
{
    int n_incre;
    n_incre=getint();// scanf("%d", &n_incre);
    for (int i = 1; i <= n_incre; ++i) {
        int object_id,tag;
        object_id=getint();tag=getint();// scanf("%d%d",&object_id,&tag);
        object[object_id].tag = tag;
    }
}
void init(int round)
{
    Round=round;
    timestamp=0;
    memset(disk, 0, sizeof(disk));
    memset(unit_num, 0, sizeof(unit_num));
	for (int i = 1; i <= N; i++)
		disk_point[i][0]=disk_point[i][1] = {1,80};
    for (int i = 0; i < MAX_OBJECT_NUM; i++)
    {
        object[i].last_request_point=0;
        memset(object[i].replica,0,sizeof(object[i].replica));
    }
    if(round == 1)
    {
        for(int i=1;i<=G+100;i++)
        {
            int cost=80,remain=i;
            dis_canread[i]=0;
            while(remain>=max((int)ceil(cost*0.8),16))
            {
                dis_canread[i]++;
                cost=max((int)ceil(cost*0.8),16);
                remain-=cost;

                if(cost==16) dis_canread[i]+=remain/cost,remain%=cost;
            }
        }
    }
    if(round==2)
    {
        for(int i=1;i<=T+EXTRA_TIME;i++){
            for(int tag=1;tag<=M;tag++){
                num_read[tag][i]=num_read[tag][i-1];
                num_write[tag][i]=num_write[tag][i-1];
                num_delete[tag][i]=num_delete[tag][i-1];
            }
            for(int j=1;j<=n_reads[i];j++){
                int object_id = get<1>(read_datas[i][j-1]);
                int tag = object[object_id].tag;
                num_read[tag][i]+=object[object_id].size  +1 ;
            }
            for(int j=1;j<=n_writes[i];j++){
                int object_id = get<0>(write_datas[i][j-1]);
                int tag = object[object_id].tag;
                num_write[tag][i]+=object[object_id].size;
            }
            for(int j=1;j<=n_deletes[i];j++){
                int object_id = delete_datas[i][j-1];
                int tag = object[object_id].tag;
                num_delete[tag][i]+=object[object_id].size;
            }
        }
    }
}
int Round=1;
int main()
{
    scanf("%d%d%d%d%d%d%d", &T, &M, &N, &V, &G, &K1, &K2);

    printf("OK\n");
    if(Round !=2) fflush(stdout);

    init(1);
    for (int t = 1; t <= T + EXTRA_TIME; t++) {
        timestamp_action();
        delete_action(1, t);
        write_action(1, t);
        read_action(1, t);
        if (t % FRE_PER_SLICING == 0) {
            gbCollect(1);
        }
    }
    clean();
    incre_action();

    gather_action();
    init(2);
    Sort();
	memset(sum,0,sizeof(sum));
	for (int i=1;i<=M;i++) {
		// sum[i][0]=(M-i)*2;
		for (int j=1;j<=T+EXTRA_TIME;j++)
			// sum[i][j]=sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1]+fre_write[vtag[i]][j];
			sum[i][j]=sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1]+get_num_write(vtag[i],j)-get_num_delete(vtag[i],j);
	}
    for (int t = 1; t <= T + EXTRA_TIME; t++) {
        timestamp_action();
        delete_action(2, t);
        write_action(2, t);
        read_action(2, t);
        if (t % FRE_PER_SLICING == 0) {
            gbCollect(2);
        }
    }
    clean();

    init(3);
    for (int t = 1; t <= T + EXTRA_TIME; t++) {
        timestamp_action();
        delete_action(3, t);
        write_action(3, t);
        read_action(3, t);
        if (t % FRE_PER_SLICING == 0) {
            gbCollect(3);
        }
    }
    clean();

    return 0;
}